package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import sfmi.component.chnchannel.chncallcenter.client.FreeInsuranceClient;
import sfmi.component.chnchannel.chncallcenter.dto.common.Name;
import sfmi.component.chnchannel.chncallcenter.dto.enums.ConnectMode;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FICoverage;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIPolicyholder;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIProduct;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIQuotation;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.key.FIQuotationKey;

public class findQuotation extends HttpServlet {
	private static final Log log=LogFactory.getLog(findQuotation.class);
	/**
	 * Constructor of the object.
	 */
	public findQuotation() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String returnxml="";
		try{
			String QuotationNo=Util.getString(request.getParameter("QuotationNo"));
			log.debug("QuotationNo:"+QuotationNo);
			String CouponNo=Util.getString(request.getParameter("CouponNo"));
			log.debug("CouponNo:"+CouponNo);
			String type=Util.getString(request.getParameter("type"));
			log.debug("type:"+type);
			String IP=Util.getIp(request);
			log.debug("IP:"+IP);
			FIQuotationKey fiQuotationKey1 = new FIQuotationKey();
            if(type.equals("Q")){
            	fiQuotationKey1.setQuotationNo(QuotationNo);
            }else if(type.equals("C")){
            	fiQuotationKey1.setCouponNo(CouponNo);
            }    
		    fiQuotationKey1.setIpAddress(IP);
		    FreeInsuranceClient freeInsuranceClient = new FreeInsuranceClient(ConnectMode.RealServer);
		    FIQuotation f=freeInsuranceClient.findQuotation(fiQuotationKey1);
		    Document result=Util.getDoc();
        	Element root=result.addElement("Result");
		    log.debug(f.getCouponId());
		    root.addElement("CouponId").addText(Util.getString(f.getCouponId()));
		    log.debug(f.getCouponNo());
		    root.addElement("CouponNo").addText(Util.getString(f.getCouponNo()));
		    log.debug(f.getPolicyNo());
		    root.addElement("PolicyNo").addText(Util.getString(f.getPolicyNo()));
		    log.debug(f.getQuotationNo());
		    root.addElement("QuotationNo").addText(Util.getString(f.getQuotationNo()));
		    log.debug(f.getSelfCheckYn());
		    root.addElement("SelfCheckYn").addText(Util.getString(f.getSelfCheckYn()));
		    log.debug(f.getProduct());
		    root.addElement("Product").addText(Util.getString(f.getProduct()));
//		    log.debug(f.getStatus());
//		    root.addElement("Status").addText(Util.getString(f.getStatus()));
		    FIProduct fi=f.getProduct();
		    log.debug(fi.getInputDate());
		    root.addElement("InputDate").addText(Util.getString(fi.getInputDate()));
		    log.debug(fi.getPolicyEndDate());
		    root.addElement("PolicyEndDate").addText(Util.getString(fi.getPolicyEndDate()));
		    log.debug(fi.getPolicyStartDate());
		    root.addElement("PolicyStartDate").addText(Util.getString(fi.getPolicyStartDate()));
		    log.debug(fi.getProductCode());
		    root.addElement("ProductCode").addText(Util.getString(fi.getProductCode()));
		    log.debug(fi.getProductName());
		    root.addElement("ProductName").addText(Util.getString(fi.getProductName()));
		    log.debug(fi.getRemarks());
		    root.addElement("Remarks").addText(Util.getString(fi.getRemarks()));
		    log.debug(fi.getWrittneDate());
		    root.addElement("WrittneDate").addText(Util.getString(fi.getWrittneDate()));
		
		    List list=f.getCoverageList();
		    if(list!=null&&list.size()>0){
		    	for(int i=0;i<list.size();i++){
		    		FICoverage fi1=(FICoverage)list.get(i);
					if(fi1!=null){
						Element fii=root.addElement("fii");
						log.debug(fi1.getCode());
						fii.addElement("Code").addText(Util.getString(fi1.getCode()));
						log.debug(fi1.getDeductibleAmount());
						fii.addElement("DeductibleAmount").addText(Util.getString(fi1.getDeductibleAmount()));
						log.debug(fi1.getDeductibleCurrency());
						fii.addElement("DeductibleCurrency").addText(Util.getString(fi1.getDeductibleCurrency()));
						log.debug(fi1.getDetail());
						fii.addElement("Detail").addText(Util.getString(fi1.getDetail()));
						log.debug(fi1.getEnglishName());
						fii.addElement("EnglishName").addText(Util.getString(fi1.getEnglishName()));
						log.debug(fi1.getLolCurrency());
						fii.addElement("LolCurrency").addText(Util.getString(fi1.getLolCurrency()));
						log.debug(fi1.getLolLimitAmount());
						fii.addElement("LolLimitAmount").addText(Util.getString(fi1.getLolLimitAmount()));
						log.debug(fi1.getName());
						fii.addElement("Name").addText(Util.getString(fi1.getName()));

					}
		    	}
		    }
		    returnxml=result.asXML();
		}catch(Exception e){
			log.error(Util.getERRO(e));
		}
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=utf-8;");
	    PrintWriter out = response.getWriter();
	    log.debug("xml:"+returnxml);
	    out.print(returnxml);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
